package com.gitee.feizns.quickstart.web.ex.impl;

import com.gitee.feizns.quickstart.domain.vo.Ret;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpMediaTypeNotSupportedException;

/**
 * 一般是因为后端不接受前端所发送的媒体类型
 * Accept:application/json； Content-Type:application/json
 * @author feizns
 * @since 2019/7/24
 */
@NoArgsConstructor
public class HttpMediaTypeNotSupportedExceptionHandler extends AbstractExceptionHandler<HttpMediaTypeNotSupportedException> {

    /**
     * 响应的Http状态
     */
    private HttpStatus httpStatus = HttpStatus.BAD_REQUEST;

    public HttpMediaTypeNotSupportedExceptionHandler(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
    }

    @Override
    public ResponseEntity<Ret<?>> handle(HttpMediaTypeNotSupportedException e) {
        return ResponseEntity.status(httpStatus)
                .body(Ret.error(HttpStatus.BAD_REQUEST.value(), e.getMessage()));
    }

}
